<?php
/**
 * Created by PhpStorm.
 * User: 86158
 * Date: 2021/7/10
 * Time: 11:37
 */

namespace App\Services\exam;


use App\Models\exams\ExamLog;
use App\Models\exams\ExamQuestion;
use App\Models\exams\UserExam;

class ExamService
{
    /**
     * 获取下一题
     * @param $exam_id
     * @param int $exam_question_id
     * @return mixed
     */
    public function getNextQuestion($exam_id,$exam_question_id=0)
    {
        $data = ExamQuestion::with('question')
            ->where('status',1)
            ->where('exam_id',$exam_id)
            ->when($exam_question_id,function ($query,$exam_question_id){
                $query->where('id','>',$exam_question_id);
            })
            ->first();
        $data['total'] = ExamQuestion::where('exam_id',$exam_id)->where('status',1)->count();
        return $data;
    }

    public function checkCommit($user_exam_id,$exam_id)
    {
        $exam_question_count = ExamQuestion::where('exam_id',$exam_id)->where('status',1)->count();
        $user_exam_log_count = ExamLog::where('user_exam_id',$user_exam_id)->count();
        if($exam_question_count != $user_exam_log_count){
            return false;
        }
        return true;
    }

    public static function calculate($user_exam_id)
    {
        $total_score = ExamLog::where('user_exam_id',$user_exam_id)->where('correct',1)->sum('score');
        return $total_score;
    }
}